#include "wspace_switcher.h"
#include "wspace_button.h"
#include "launchers_pad.h"
#include "globals.h"


GtkWidget * wspace_buttons[MAX_WSPACES];
gulong      wspace_buttons_handlers[MAX_WSPACES];


/**
 * wspace_switcher_new:
 *
 * Create workspace buttons. Buttons is are GtkRadioButton widgets.
 * Pack them into GtkHBox.
 *
 * Return value: a new GtkWidget.
 **/
GtkWidget *wspace_switcher_new ()
{
    GtkWidget * wspaces_box;
    GSList *    radio_group = NULL;
    guint       wspace_count;
    guint       i;

    // workspaces buttons box
    wspaces_box = gtk_hbutton_box_new ();
    gtk_box_set_spacing (GTK_BOX (wspaces_box), 2);
    gtk_button_box_set_layout (GTK_BUTTON_BOX (wspaces_box), GTK_BUTTONBOX_CENTER);


    // create and add workspace buttons
    wspace_count = wnck_screen_get_workspace_count (screen);

    for (i = 0; i < wspace_count; i++)
    {
        wspace_buttons[i] = wspace_button_new (i);

        gtk_radio_button_set_group (GTK_RADIO_BUTTON (wspace_buttons[i]), radio_group);
        radio_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (wspace_buttons[i]));

        gtk_box_pack_start (GTK_BOX(wspaces_box), wspace_buttons[i], TRUE, TRUE, 0);
    }

    // connect sybnal - added for fix problem with activate wspace on create radio button
    for (i = 0; i < wspace_count; i++)
        wspace_buttons_handlers[i] = g_signal_connect (G_OBJECT (wspace_buttons[i]),
                                     "clicked",
                                     G_CALLBACK (wspace_button_on_clicked),
                                     GUINT_TO_POINTER(i));

    return wspaces_box;
}


/**
 * wspace_switcher_sync:
 *
 * Detect current workspace and activate appropriate button.
 **/
void wspace_switcher_sync()
{
    WnckWorkspace * wspace;
    const gchar *   wspace_name;
    guint           wspace_count;
    guint           id;


    wspace = wnck_screen_get_active_workspace (screen);
    wspace_name = wnck_workspace_get_name (wspace);
    launchers_pad_load_launchers ();

    id = wnck_workspace_get_number (wspace);

    wspace_count = wnck_screen_get_workspace_count(screen);

    wspace_switcher_block_signals (wspace_count);

    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wspace_buttons[id]), TRUE);

    wspace_switcher_unblock_signals (wspace_count);
}


/**
 * wspace_switcher_on_change_wspace:
 *   @screen:  WnckScreen *
 *   @previously_active_space:  WnckWorkspace *
 *   @user_data:  gpointer *
 *
 * Event. Emited when changed workspace by external source.
 * It need for change state of workspace switcher.
 **/
void wspace_switcher_on_change_wspace (
    WnckScreen    *screen,
    WnckWorkspace *previously_active_space,
    gpointer       user_data)
{
    wspace_switcher_sync();
}


/**
 * wspace_switcher_block_signals:
 *
 * Block on_clicked signals on all workspace buttons.
 **/
void wspace_switcher_block_signals ()
{
    guint wspace_count;
    guint i;

    wspace_count = wnck_screen_get_workspace_count (screen);

    for (i = 0; i < wspace_count; i++)
        g_signal_handler_block (G_OBJECT (wspace_buttons[i]), wspace_buttons_handlers[i]);
}


/**
 * wspace_switcher_unblock_signals:
 *
 * Unblock on_clicked signals on all workspace buttons.
 **/
void wspace_switcher_unblock_signals ()
{
    guint wspace_count;
    guint i;

    wspace_count = wnck_screen_get_workspace_count (screen);

    for (i = 0; i < wspace_count; i++)
        g_signal_handler_unblock (G_OBJECT (wspace_buttons[i]), wspace_buttons_handlers[i]);
}
